package com.stn.zjx.givingtreasure.utils;



import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.text.DecimalFormat;
import java.util.Arrays;

import codec.binary.Base64;


public class Base64FileUtils {
     public static  final int IMAGE=0;
    public static  final int MUSIC=1;
    public static  final int VIDEO=2;
    public static  final int ZIP=3;
    public static  final int TORRENT=4;
    public static  final int TXT=5;
    public static  final int EXE=6;

    //获取文件的base64字符串
    public static String encryptToBase64(String filePath) {
        if (filePath == null) {
            return null;
        }
        try {
            ByteArrayOutputStream os1 = new ByteArrayOutputStream();
            InputStream file1 = new FileInputStream(filePath);
            byte[] byteBuf = new byte[3*1000];
            byte[] base64ByteBuf;
            int count1; //每次从文件中读取到的有效字节数
            while((count1=file1.read(byteBuf)) != -1)
            {
                if(count1!=byteBuf.length) //如果有效字节数不为3*1000，则说明文件已经读到尾了，不够填充满byteBuf了
                {
                    byte[] copy = Arrays.copyOf(byteBuf, count1); //从byteBuf中截取包含有效字节数的字节段
                    base64ByteBuf = Base64.encodeBase64(copy); //对有效字节段进行编码
                }
                else
                {
                    base64ByteBuf = Base64.encodeBase64(byteBuf);
                }
                os1.write(base64ByteBuf, 0, base64ByteBuf.length);
                os1.flush();
            }
            file1.close();

            return os1.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


   //过滤字符串data:image/png;base64
    public static  String getBase64Replece(String basestr){
             return     basestr
                        .replace("data:image/png;base64,","")
                        .replace("data:image/jpg;base64,","")
                        .replace("data:image/gif;base64,","")
                        .replace("data:image/jpeg;base64,","")
                        .replace("data:image/PNG;base64,","")
                        .replace("data:image/JPG;base64,","")
                        .replace("data:image/GIF;base64,","")
                        .replace("data:image/JPEG;base64,","")
                           .replace("data:music/map3;base64,","")
                           .replace("data:music/wav;base64,","")
                           .replace("data:music/MP3;base64,","")
                           .replace("data:music/WAV;base64,","")
                         .replace("data:video/avi;base64,","")
                        .replace("data:video/rm;base64,","")
                        .replace("data:video/map4;base64,","")
                        .replace("data:video/flv;base64,","")
                        .replace("data:video/AVI;base64,","")
                        .replace("data:video/RM;base64,","")
                        .replace("data:video/MAP4;base64,","")
                        .replace("data:video/FLV;base64,","")
                        .replace("data:zip/zip;base64,","")
                        .replace("data:zip/rar;base64,","")
                        .replace("data:zip/ZIP;base64,","")
                        .replace("data:zip/RAR;base64,","")
                        .replace("data:torrent/torrent;base64,","")
                        .replace("data:txt/txt;base64,","")
                        .replace("data:txt/TXT;base64,","")
                        .replace("data:exe/exe;base64,","")
                        .replace("data:exe/EXE;base64,","");
    }


    public static  long getBase64Length(String basestr){
             int   index=basestr.indexOf("=");
          String news=basestr.substring(0,index);
          return (news.length()-(news.length()/8)*2);
    }
    //自动转换文件大小
    public static String getNetFileSizeDescription(long size) {
        StringBuffer bytes = new StringBuffer();
        DecimalFormat format = new DecimalFormat("###.00");
        if (size >= 1024 * 1024 * 1024) {
            double i = (size / (1024.0 * 1024.0 * 1024.0));
            bytes.append(format.format(i)).append("GB");
        }
        else if (size >= 1024 * 1024) {
            double i = (size / (1024.0 * 1024.0));
            bytes.append(format.format(i)).append("MB");
        }
        else if (size >= 1024) {
            double i = (size / (1024.0));
            bytes.append(format.format(i)).append("KB");
        }
        else if (size < 1024) {
            if (size <= 0) {
                bytes.append("0B");
            }
            else {
                bytes.append((int) size).append("B");
            }
        }
        return bytes.toString();
    }

    public static String getFileTypestr(int type){
         String str="";
         switch (type){
             case IMAGE:
                 str="images";
                 break;
             case MUSIC:
                 str="musics";
                 break;
             case VIDEO:
                 str="video";
                 break;
             case ZIP:
                 str="zip";
                 break;
             case TORRENT:
                 str="torrent";
                 break;
             case TXT:
                 str="txt";
                 break;
             case EXE:
                 str="exe";
                 break;
         }
        return  str;
    }

}
